package com.wbdgj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.coupon.CouponDetailAbleActivity;
import com.wbdgj.ui.home.YhqDetailMyActivity;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.mine.LotteryWebViewActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import retrofit2.Call;

/**
 * ListAdapter
 * Created by XiaoWei on 2015-11-14.
 */
public class YhqMyNewAdapter extends BaseListAdapter<LinkedTreeMap> {

    public YhqMyNewAdapter(Context context, List<LinkedTreeMap> objects) {
        super(context, objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (getItemViewType(position) == 0) {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.layout_has_no_data, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            } else {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.ui_wb_yhq_item_my, parent, false);
                holder.money_wf = (TextView) convertView.findViewById(R.id.money_wf);
                holder.content_wf=(TextView) convertView.findViewById(R.id.content_wf);
                holder.lx_wf=(TextView) convertView.findViewById(R.id.lx_wf);
                holder.hot=(ImageView) convertView.findViewById(R.id.hot);
                holder.title_wf=(TextView) convertView.findViewById(R.id.title_wf);
                holder.time_wf=(TextView) convertView.findViewById(R.id.time_wf);
                holder.COUNT=(TextView) convertView.findViewById(R.id.COUNT);
                holder.ABLE_BRANCH_NAME=(TextView) convertView.findViewById(R.id.ABLE_BRANCH_NAME);
                holder.dota=(TextView) convertView.findViewById(R.id.dota);

                holder.swjImg=(ImageView) convertView.findViewById(R.id.swjImg);
                holder.swjLay = (LinearLayout) convertView.findViewById(R.id.swjLay);
                holder.ljlq_wf=(TextView) convertView.findViewById(R.id.ljlq_wf);
                holder.wfLay = (LinearLayout) convertView.findViewById(R.id.wfLay);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(),getScreenHeight()*2/3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            if (position >= 3){
                holder.hot.setVisibility(View.GONE);
            }else{
                holder.hot.setVisibility(View.VISIBLE);
            }
            if (decimalFormat.format(mDataList.get(position).get("STATUS")).equals("1")){
                holder.ljlq_wf.setText("立即使用");
            }else {
                holder.ljlq_wf.setText("不可用");
            }

            holder.money_wf.setText(decimalFormat.format(mDataList.get(position).get("AMOUNT")));
            holder.content_wf.setText("消费满"+ decimalFormat.format(mDataList.get(position).get("MIN_AMOUNT")) +"可用");
            holder.title_wf.setText(mDataList.get(position).get("NAME")+"");
            holder.lx_wf.setText(mDataList.get(position).get("TYPE_NAME")+"");
            holder.COUNT.setText("");
            holder.time_wf.setText("使用截止日期："+formatDate1(mDataList.get(position).get("USE_END")+""));
            if ((mDataList.get(position).get("ABLE_BRANCH_NAME")+"").equals("null")){
                holder.ABLE_BRANCH_NAME.setText("可用门店: "+mDataList.get(position).get("BRANCH_NAME")+"");
            }else{
                holder.ABLE_BRANCH_NAME.setText("可用门店: "+mDataList.get(position).get("ABLE_BRANCH_NAME")+"");
            }
            if (!(mDataList.get(position).get("CATEGORY")+"").equals("null")){
                if (decimalFormat.format(mDataList.get(position).get("CATEGORY")).equals("2")){
                    holder.wfLay.setBackgroundResource(R.mipmap.swj_bg);
                    Glide.with(mContext).load(HttpAdapter.yhqlUrl + "Images/branch_pic/prize/"+mDataList.get(position).get("IMG_URL")).placeholder(R.mipmap.swj_bg).error(R.mipmap.swj_bg).into(holder.swjImg);
                    holder.lx_wf.setBackgroundResource(R.drawable.shape_swj_radius);
                    holder.ljlq_wf.setBackgroundResource(R.drawable.shape_swj_k_radius);
                    holder.ljlq_wf.setTextColor(mContext.getResources().getColor(R.color.swj_k));
                    holder.money_wf.setVisibility(View.GONE);
                    holder.content_wf.setVisibility(View.GONE);
                    holder.dota.setVisibility(View.GONE);
                    holder.swjImg.setVisibility(View.VISIBLE);
                    holder.swjLay.setVisibility(View.GONE);
                }else if (decimalFormat.format(mDataList.get(position).get("CATEGORY")).equals("3")){
                    holder.wfLay.setBackgroundResource(R.mipmap.cj_icon);
                    holder.lx_wf.setBackgroundResource(R.drawable.shape_cj_radius);
                    holder.ljlq_wf.setBackgroundResource(R.drawable.shape_cj_k_radius);
                    holder.ljlq_wf.setTextColor(mContext.getResources().getColor(R.color.cj_k));
                    holder.money_wf.setVisibility(View.GONE);
                    holder.content_wf.setVisibility(View.GONE);
                    holder.dota.setVisibility(View.GONE);
                    holder.swjImg.setVisibility(View.GONE);
                    holder.swjLay.setVisibility(View.VISIBLE);
                }else{
                    holder.wfLay.setBackgroundResource(R.mipmap.yhq_wf);
                    holder.lx_wf.setBackgroundResource(R.drawable.shape_login_radius);
                    holder.ljlq_wf.setBackgroundResource(R.drawable.shape_wf_radius);
                    holder.ljlq_wf.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
                    holder.money_wf.setVisibility(View.VISIBLE);
                    holder.content_wf.setVisibility(View.VISIBLE);
                    holder.dota.setVisibility(View.VISIBLE);
                    holder.swjImg.setVisibility(View.GONE);
                    holder.swjLay.setVisibility(View.VISIBLE);
                }
            }
        }
        return convertView;
    }

    private static final class ViewHolder {
        TextView money_wf, content_wf, title_wf, time_wf, COUNT, ABLE_BRANCH_NAME, dota;
        TextView ljlq_wf , lx_wf;
        ImageView hot, swjImg;
        LinearLayout noDataRootLayout, wfLay, swjLay;
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.widthPixels;
    }

    private int getScreenHeight() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.heightPixels;
    }

    public static String formatDate1(String dateStr) {
        DateFormat formatFrom = new SimpleDateFormat("MMM dd,yyyy", Locale.ENGLISH);
        Date date = null;
        try {
            date = formatFrom.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        DateFormat formatTo = new SimpleDateFormat("yyyy/MM/dd");
        return formatTo.format(date)+"";
    }

    private void appCoupongetDetail(String id) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().appCoupongetDetail("",id,  BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(mContext) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(mContext, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }
}
